﻿Imports Training.Common

Partial Public Class AllUsers
    Inherits System.Web.UI.Page

    ''' <summary>
    ''' Page load event. Make sure user is SuperAdmin or redirect
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not User.IsInRole("superadmin") Then
            Response.Redirect("Default.aspx")
        End If
    End Sub

    ' **** THE CODE BELOW IS THIRD PARTY AND DOCUMENTATION IS
    ' **** PROVIDED AS-IS BY THE AUTHOR OF THIS CODE

    'This event handler is fired whenever the user clicks the IsApproved CheckBox
    Protected Sub ToggleApproved(ByVal sender As Object, ByVal e As EventArgs)
        'Reference the checkbox
        Dim isApproved As CheckBox = CType(sender, CheckBox)

        'Reference the GridViewRow used by the CheckBox
        Dim gvr As GridViewRow = CType(isApproved.Parent.Parent, GridViewRow)

        'Get the user information
        Dim userName As String = GridView1.DataKeys(gvr.RowIndex).Value
        Dim userInfo As MembershipUser = Membership.GetUser(userName)

        'Set the value...
        userInfo.IsApproved = isApproved.Checked

        'Update the record
        Membership.UpdateUser(userInfo)
    End Sub

    'This event handler is fired whenever the user clicks the IsAdministrator CheckBox
    Protected Sub ToggleAdministrator(ByVal sender As Object, ByVal e As EventArgs)
        'Reference the checkbox
        Dim isAdministrator As CheckBox = CType(sender, CheckBox)

        'Reference the GridViewRow used by the CheckBox
        Dim gvr As GridViewRow = CType(isAdministrator.Parent.Parent, GridViewRow)

        'Get the user information
        Dim userName As String = GridView1.DataKeys(gvr.RowIndex).Value
        Dim userInfo As MembershipUser = Membership.GetUser(userName)

        'Set the value...
        Const ADMIN_ROLE_NAME As String = "Administrator"
        If isAdministrator.Checked Then
            'Add the user to the role
            Roles.AddUserToRole(userName, ADMIN_ROLE_NAME)
        Else
            'Remove the user from the role
            Roles.RemoveUserFromRole(userName, ADMIN_ROLE_NAME)
        End If
    End Sub

    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
        'We need to unlock the specified user
        If e.CommandName = "UnlockUser" Then
            'Reference the checkbox
            Dim unlockLinkButton As LinkButton = CType(e.CommandSource, LinkButton)

            'Reference the GridViewRow used by the CheckBox
            Dim gvr As GridViewRow = CType(unlockLinkButton.Parent.Parent, GridViewRow)

            'Get the user information
            Dim userName As String = GridView1.DataKeys(gvr.RowIndex).Value
            Dim userInfo As MembershipUser = Membership.GetUser(userName)

            'Unlock the user
            userInfo.UnlockUser()

            'Rebind the data to the GridView
            GridView1.DataBind()
        End If
    End Sub
End Class